<?php

namespace app\common\model\lives;

use think\Model;

class Lives extends Model {

    protected $dbName = 'lives';
    protected $connection = ['database' => 'lives'];

    public function getlist($param) {
        if (empty($param['keywords'])) {
            $res = $this->alias('l')
                    ->join('live_delivery_num ld', 'l.id=ld.live_id', 'left')
                    ->join('live_share_num ls', 'l.id=ls.live_id', 'left')
                    ->field('l.id,l.title,l.roomid,l.type,ld.live_delivery_num,ld.click_person_num,ls.live_share_num,ls.share_person_num')
                    ->order('l.id desc')
                    ->limit(($param['page'] - 1) * $param['pageSize'], $param['pageSize'])
                    ->select();
            $res1 = $this->alias('l')
                    ->join('live_delivery_num ld', 'l.id=ld.live_id', 'left')
                    ->join('live_share_num ls', 'l.id=ls.live_id', 'left')
                    ->field('l.id,l.title,l.roomid,l.type,ld.live_delivery_num,ld.click_person_num,ls.live_share_num,ls.share_person_num')
                    ->order('l.id desc')
                    ->select();
//        return $this->getLastSql();exit;
        } else {
            $res = $this->alias('l')
                    ->join('live_delivery_num ld', 'l.id=ld.live_id', 'left')
                    ->join('live_share_num ls', 'l.id=ls.live_id', 'left')
                    ->field('l.id,l.title,l.roomid,l.type,ld.live_delivery_num,ld.click_person_num,ls.live_share_num,ls.share_person_num')
                    ->where(['l.title' => ['like', '%' . $param['keywords'] . '%']])
                    ->order('l.id desc')
                    ->limit(($param['page'] - 1) * $param['pageSize'], $param['pageSize'])
                    ->select();
//                    return $this->getLastSql();exit;
            $res1 = $this->alias('l')
                    ->join('live_delivery_num ld', 'l.id=ld.live_id', 'left')
                    ->join('live_share_num ls', 'l.id=ls.live_id', 'left')
                    ->field('l.id,l.title,l.roomid,l.type,ld.live_delivery_num,ld.click_person_num,ls.live_share_num,ls.share_person_num')
                    ->order('l.id desc')
                    ->where(['l.title' => ['like', '%' . $param['keywords'] . '%']])
                    ->select();
        }
//        return 111;
        $data['res'] = $res;
        $data['count'] = count($res1);
        return $data;
    }

    //提供需要修改的直播间数据
    public function edit_lives($param) {
        return $res = $this->alias('l')->where(['id' => $param['id']])->field('l.title,l.id,l.roomid,l.type')->select();
    }

    //提供需要修改的广告数据
    public function edit_adv_place($param) {
        return $res = $this->setTable('adv_place')->where(['lives_id' => $param['id']])->select();
    }

    //提供需要修改的导航栏数据
    public function edit_nav($param) {
        return $res = $this->setTable('nav')->where(['live_id' => $param['id']])->field('nav.navname,nav.live_id,nav.ifshow')->select();
    }

    //新增直播间
    public function add_live($param) {
        return $res = $this->insertGetId($param);
    }

    //新增广告位
    public function add_adv_place($param) {
        return $res = $this->setTable('adv_place')->insertGetId($param);
    }

    //新增栏位
    public function add_nav($param) {
        return $res = $this->setTable('nav')->insertGetId($param);
    }

    //修改直播间
    public function update_lives($param, $where) {
        return $res = $this->where(['id' => $where])->update($param);
    }

    //修改广告位
    public function update_adv_place($param, $where) {
        return $res = $this->setTable('adv_place')->where(['lives_id' => $where])->update($param);
    }

    //删除广告位
    public function delete_adv_place($param) {
        return $res = $this->setTable('adv_place')->where(['lives_id' => $param])->delete();
    }

    public function update_nav($where1, $where2, $param) {
        return $res = $this->setTable('nav')->where(['navname' => $where1, 'live_id' => $where2])->update(['ifshow' => $param]);
    }

    public function add_recommend($param) {
        return $res = $this->setTable('recommend_place')->insertGetId($param);
    }

    public function getrecommendlist($param){
        if(empty($param['keywords'])){
            $res = $this->setTable('recommend_place r')
                     
                     ->order('r.orders')
                     ->limit(($param['page']-1)*$param['pageSize'],$param['pageSize'])
                     ->select();
             $res1 = $this->setTable('recommend_place r')
                      
                     ->order('r.orders')
                     ->select();
               $sql = "SELECT r.id,r.title,r.orders,r.ifshow,sum(lr.recommend_click_num) cnum,sum(distinct lr.recommend_person_num) pnum 
from recommend_place r LEFT JOIN live_recommend_num  lr on (r.id=lr.recommend_id) GROUP BY lr.recommend_id order by r.orders";
               $res2 = $this->query($sql);
             $data['res'] = $res;
             $data['count'] = count($res1);
             $data['cnum'] = $res2;
             return $data;
        }else{
            $res = $this->setTable('recommend_place r')
                    ->where(['title'=>['like','%'.$param['keywords'].'%']])
                    ->order('r.orders')
                    ->limit(($param['page']-1)*$param['pageSize'],$param['pageSize'])
                    ->select();
           $res1 = $this->setTable('recommend_place r')
                    ->where(['title'=>['like','%'.$param['keywords'].'%']])
                    ->order('r.orders')
                    ->select();
           $sql = "SELECT r.id,r.title,r.orders,r.ifshow,sum(lr.recommend_click_num) cnum,sum(distinct lr.recommend_person_num) pnum 
from recommend_place r LEFT JOIN live_recommend_num  lr on (r.id=lr.recommend_id) GROUP BY lr.recommend_id order by r.orders";
               $res2 = $this->query($sql);
           $data['res'] = $res;
           $data['count'] = count($res1);
           $data['cnum'] = $res2;
           return $data;
        }
    }

    public function getrecommendcount() {
        return $res = $this->setTable('recommend_place')->select();
    }

    public function edit_recommend($param) {
        return $res = $this->setTable('recommend_place')->where(['id' => $param['id']])->select();
    }

    public function update_commend($param) {
        return $res = $this->setTable('recommend_place')->where(['id' => $param['id']])->update($param[0]);
    }

    //修改推荐位排序
    public function changeorder($param) {
        $res1 = $this->setTable('recommend_place')->where(['id' => $param['this_ids']])->update(['orders' => $param['next_order']]);
        $res2 = $this->setTable('recommend_place')->where(['id' => $param['next_ids']])->update(['orders' => $param['this_order']]);
    }

    public function changeorderup($param) {
        $res1 = $this->setTable('recommend_place')->where(['id' => $param['this_ids']])->update(['orders' => $param['prev_order']]);
        $res2 = $this->setTable('recommend_place')->where(['id' => $param['prev_ids']])->update(['orders' => $param['this_order']]);
    }

    //获取所有的投放
    public function add_delivery($param) {
        $res = $this->setTable('delivery')->insertGetId($param);
        return $res;
    }

    //获取当前直播间绑定的渠道
    public function getlivedelivery($param) {
        return $res = $this->setTable('delivery')->where(['lives_id' => $param['id']])->field('id,channel_name,status')->select();
    }

    public function getindex() {
        $res = $this->setTable('recommend_place')->where(['ifshow' => 1])->order('orders')->select();
        return $res;
    }

//获取渠道列表
    public function getdeliverylist($param) {
              $res = $this->setTable('delivery')->alias('d')
                ->join('live_delivery_num ld','d.id=ld.delivery_id and d.lives_id=ld.live_id','left')
               ->join('lives l','l.id=d.lives_id','left')
              ->field('l.type,d.id,d.channel_name,d.channel_id,d.add_time,d.status,ld.live_delivery_num,ld.click_person_num')
               ->where(['d.lives_id'=>$param['id']])
               ->select();
       return $res;
    }

    //修改投放状态
    public function edit_delivery($param) {
        if ($param['status'] == 1) {
            return $res = $this->setTable('delivery')->where(['id' => $param['id']])->update(['status' => 0]);
        } else {
            return $res = $this->setTable('delivery')->where(['id' => $param['id']])->update(['status' => 1]);
        }
    }

    //获取前台的聊天数据
    public function getChatlog($param) {
//        exit;
//        $res = $this->setTable('public_chat')->where(['live_id'=>"{$param['lid']}",'id'=>['gt',"{$param['maxId']}"]])->limit(100)->select();
//        return $res;
        $redisKey = 'data_public_chat_' . $param['lid'];

        if ($param['maxId'] == 0) {
            $redisStart = (int) ('-' . $param['step']);
            $redisStep = -1;
        } else {
            $redisStart = $param['maxId'];
            $redisStep = $param['maxId'] + $param['step'];
        }

        //$publicChat = \think\Cache::lrange($redisKey, $redisStart, $redisStep);
        $publicChatLen = \think\Cache::llen($redisKey);

        if (!$publicChatLen) {
            $res = $this->setTable('public_chat')->where(['live_id' => $param['lid']])->order('id desc')->limit(500)->select();

            foreach ($res as $value) {
                $value = json_encode($value);
                \think\Cache::lpush($redisKey, $value);
            }

            //$publicChat = \think\Cache::lrange($redisKey, $redisStart, $redisStep);
        }
        
        $publicChat = \think\Cache::lrange($redisKey, $redisStart, $redisStep);
        $publicChatLen = \think\Cache::llen($redisKey);
        
        $startId = 0;
        foreach ($publicChat as $value) {
            $value = json_decode($value, true);

            if ($redisStart < 0) {
                $value['start_id'] = ((($publicChatLen < $param['step']) ?$param['step']:$publicChatLen) -$param['step']+1) + $startId;
            } else {
                $value['start_id'] = $redisStart + $startId +1;
            }


            $publicChatArr[] = $value;
            $startId++;
        }

        return $publicChatArr;
    }

    public function getnav($param) {
        $res = $this->setTable('nav')->where(['live_id' => $param['live_id'], 'ifshow' => 1])->cache(600)->select();
        return $res;
    }

    //获取广告位
    public function getAdv($param) {
        $res = $this->setTable('adv_place')->where(['lives_id' => $param['lives_id']])->order('orders asc')->cache(600)->select();
        return $res;
    }

    public function getDelivery($param) {
//        return $param;
        $res = $this->setTable('delivery')->where(['lives_id' => $param['lid'], 'channel_id' => $param['did']])->cache(600)->select();
        return $res;
    }

    //添加投放次数
    public function adddelivery($param) {
        $res = $this->setTable('delivery_click_num')->insertGetId($param);
        return $res;
    }

    //添加分享次数
    public function addshare($param) {
        $res = $this->setTable('delivery_share_num')->insertGetId($param);
        return $res;
    }

    public function getroom($param) {
        $res = $this->setTable('lives')->where(['id' => $param])->select();
        return $res;
    }

    public function check($param) {
        $res = $this->setTable('delivery')->where(['lives_id' => $param['lid'], 'channel_id' => $param['did']])->select();
        return $res;
    }

    public function getreportform($param) {
        $data['click'] = $this->setTable('live_delivery_num')->where(['live_id' => $param['lid'], 'delivery_id' => $param['did']])->group('date')->select();
        $data['share'] = $this->setTable('live_share_num')->where(['live_id' => $param['lid'], 'delivery_id' => $param['did']])->group('date')->select();
        return $data;
    }

    public function addrecommend($param) {

        $res = $this->setTable('recommend_click_num')->insertGetId($param);
        return $res;
    }
    public function rreportform($param){
//        $sql = "select lr.recommend_click_num,lr.recommend_person_num,lr.date from live_recommend_num lr where recommend_id=$param['rid'] group BY date";
        $res = $this->setTable('live_recommend_num lr')
                ->field('lr.recommend_click_num,lr.recommend_person_num,lr.date')
                ->where(['recommend_id'=>$param['rid']])
                ->group('date')
                ->select();
        return $res;
    }
     public function getdeliverylistss($param) {
        $sql1 = "select d.channel_name,d.channel_id,d.lives_id,d.status,d.id 
 from live_delivery_num ld LEFT JOIN delivery d 
on ld.live_id=d.lives_id
 where ld.live_id=177  
GROUP by d.channel_id";
        $res1 = $this->query($sql1);
            $sql2 = "SELECT live_delivery_num.live_id,live_delivery_num.delivery_id,sum(click_person_num) as click_person_num,sum(live_delivery_num) as live_delivery_num,
sum(live_share_num.live_share_num) as live_share_num,SUM(live_share_num.share_person_num) as  share_person_num 
from live_delivery_num LEFT JOIN live_share_num 
on live_delivery_num.live_id = live_share_num.live_id and live_delivery_num.delivery_id = live_share_num.delivery_id
WHERE live_delivery_num.live_id = {$param['id']}
GROUP BY live_delivery_num.delivery_id";
$res2 = $this->query($sql2);
$data['res1'] = $res1;
$data['res2'] = $res2;
        return $data;
    }
}
